package com.baizhi.action;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

import cn.dsna.util.images.ValidateCode;

/**
 * @author 王雪洋
 * @Time 2017年4月30日 上午10:11:37
 * @disc:本类主要提供登录注册相关的action方法
 */
public class UserAction extends ActionSupport {
	private User user = new User();
	private UserService us = new UserServiceImpl();
	// 验证码
	private String vcode; 
	// 邮箱激活码
	private String emailCode;
	private String code;
	// 验证邮箱的结果
	private String result;
	private String useremail;

	/*
	 * 用户登录
	 */
	public String userLogin() {
		// 收参：邮箱，用户密码，用户昵称
		User u = us.selectUserByEmail(user.getUseremail(), user.getUserpassword());
		//验证是否为普通登录
		HttpSession session = ServletActionContext.getRequest().getSession();
		String userflag = (String) session.getAttribute("userflag");
		if(u != null){
			// 判断是否激活
			if (u.getUserflag().equals("Y")) {
				if (u != null) {
					// 将用户登录信息存放session
					session.setAttribute("user", u);
					if(userflag != null){
						return "nomalsuccess";
					}else{
						session.setAttribute("userflag", "T");
						return SUCCESS;
					}
				}
			}
		}
		return ERROR;
	}

	/*
	 * 用户注册
	 */
	public String userRegiste() {
		String code = (String) ServletActionContext.getRequest().getSession().getAttribute("vcode");
		// 获取session
		HttpSession session = ServletActionContext.getRequest().getSession();
		int i = 0;
		if (!code.isEmpty() && !vcode.isEmpty() && code.equals(vcode)) {
			// 验证码验证通过
			i = us.addUser(user);
			// 获得用户ID
			if (i > 0) {
				emailCode = UUID.randomUUID().toString().replace("-", "");
				session.setAttribute("emailCode", emailCode);
				session.setAttribute("useremail", user.getUseremail());
				// 保存生成的用户ID
				session.setAttribute("iid", i);
				System.out.println("注册成功！");
			}
		} else {
			System.out.println("注册失败！");
		}

		return SUCCESS;
	}

	/*
	 * 验证邮箱是否通过
	 */
	public String checkEmailCode() {
		// 获取session
		HttpSession session = ServletActionContext.getRequest().getSession();
		String emailCode = (String) session.getAttribute("emailCode");
		Integer iid = (Integer) session.getAttribute("iid");
		if (iid != null && !code.isEmpty() && !emailCode.isEmpty() && code.equals(emailCode)) {
			session.removeAttribute("useremail");
			// 验证通过更新用户的账户状态
			user.setUserflag("Y");
			user.setUserid(iid);
			if (iid != null) {
				if (user != null) {
					// 更新账户状态成功
					if (us.updateUser(user) > 0) {
						user = us.findUserByUserid(iid);
						//将用户信息存放在session实现直接登录
						session.setAttribute("user", user);
					}
				}
			}
		}else{
			useremail = (String) session.getAttribute("useremail");
			result = "false";
			return "registerfalse";
		}

		return SUCCESS;
	}

	/*
	 * 生成验证码
	 */
	public String createCode() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		ValidateCode code = new ValidateCode(90, 30, 4, 10);
		String vcode = code.getCode().toLowerCase();
		request.getSession().setAttribute("vcode", vcode);
		code.write(response.getOutputStream());
		return null;
	}

	/*
	 * 检查邮箱是否已存在
	 */
	public String checkUserEmail() {
		System.out.println(useremail);
		user = us.selectUserEmail(useremail);
		if (user != null) {
			result = "f";
		} else {
			result = "t";
		}
		return SUCCESS;
	}

	/*
	 * 验证验证码输入是否正确
	 */
	public String checkVcode() {
		String code1 = (String) ServletActionContext.getRequest().getSession().getAttribute("vcode");
		if (!code1.isEmpty() && !code.isEmpty() && code.equals(code1)) {
			result = "t";
		} else {
			result = "f";
		}
		return SUCCESS;
	}
/*
 * 用户退出登录
 */
public String userLogout(){
	//销毁全部session
	ServletActionContext.getRequest().getSession().invalidate();
	return SUCCESS;
}
	/*
	 * 为成员变量提供setter/getter方法
	 */
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getVcode() {
		return vcode;
	}

	public void setVcode(String vcode) {
		this.vcode = vcode;
	}

	public String getEmailCode() {
		return emailCode;
	}

	public void setEmailCode(String emailCode) {
		this.emailCode = emailCode;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getUseremail() {
		return useremail;
	}

	public void setUseremail(String useremail) {
		this.useremail = useremail;
	}
}
